package com.user.dbdao;

import com.data.bean.RegistMassage;
import com.data.dbdao.BaseDao;
import com.data.util.FinalStringUtils;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

/**
 * 用户后台管理数据库层
 *
 * @author:HingLo
 * @create 2017-09-22 10:32
 **/
@Repository
@Transactional
public class IndexUserDao extends BaseDao {

    /**
     * 根据用户的id，查找软件的信息
     *
     * @param id
     * @param state
     * @return
     */
    public List<?> findPassSoftwareDao(String id, int state) {
        String sql = "select a.id,a.name,a.size,a.ctime ,a.uid ,(SELECT d.down from AppDownload as d where d.id=a.id) as down,a.state,a.md5 from App as a where a.uid=?";
        return super.findMysqlMethodDao(sql, id);
    }

    /**
     * 查询开发者信息
     *
     * @param id
     * @param type
     * @return
     */
    public List<?> lookDeveloperMassage(String id, int type) {
        String hql = "";
        if (type == FinalStringUtils.COMPANY) {
            hql = "select  r.name,r.number,r.state,c.email,c.addr,c.estate,c.phone,c.url,c.name,c.work from RegistMassage as r,CompanyMassage as c where r.id=? and r.id=c.id";
        } else if (type == FinalStringUtils.PERSON) {
            hql = "select  r.name,r.number,r.state,c.email,c.addr,c.estate,c.phone,c.url from RegistMassage as r,PersonMassage as c where r.id=? and r.id=c.id";
        } else {
            return null;
        }
        return super.findMassageDao(hql, id);
    }

    public RegistMassage developerIconDao(String id) {
        String hql = "from RegistMassage where id=?";
        List<RegistMassage> list = (List<RegistMassage>) super.findMassageDao(hql, id);
        return list.isEmpty() ? null : list.get(0);
    }

    /**
     * 跟换账号邮箱，跟换邮箱前，检测邮箱是否被占用
     *
     * @param id
     * @param code
     * @param email
     * @return
     */
    public boolean changeEmailDao(String id, String code, String email) {
        String sql = "from Account where email=?";
        if (super.findMassageDao(sql, email).isEmpty()) {
            String hql = "update Account set code=? where id=?";
            return super.updateOneMssageDao(hql, code, id);
        }
        return false;
    }

    /**
     * 激活跟换的邮箱
     *
     * @param email
     * @param code
     * @return
     */
    public boolean activeEmailChangeDao(String email, String code) {
        String hql = "update Account set code=? ,email=? where code=?";
        return super.updateOneMssageDao(hql, null, email, code);
    }
}
